﻿using System;
using Castle.MonoRail.Framework;
using GettingStartedSample.Models;

namespace GettingStartedSample.Controllers
{
    [Layout("default"), Rescue("generalerror")]
    public class ActivityController : SmartDispatcherController
    {
        public void New()
        {
        }

        public void Create([DataBind("activity")] Activity activity)
        {
            activity.Create();
            RedirectToAction("list");
        }

        public void List()
        {
            PropertyBag["activities"] = Activity.FindAll();
        }

        public void Edit(int id)
        {
            PropertyBag["activity"] = Activity.FindById(id);
        }

        public void Update([DataBind("activity")] Activity activity)
        {
            try
            {
                activity.Update();
                RedirectToAction("list");
            }
            catch (Exception ex)
            {
                Flash["error"] = ex.Message;
                Flash["activity"] = activity;

                RedirectToAction("edit", "id=" + activity.Id);
            }
        }
    }
}